<!DOCTYPE html>
<html>

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet" href="../css/mui.min.css">
    <link href="../css/mui.picker.css" rel="stylesheet" />
    <link href="../css/mui.poppicker.css" rel="stylesheet" />
    <link rel="stylesheet" href="../css/common.css" />
    <title>库存</title>
    <style>
      html {
        font-family: Roboto, Arial, "Noto Sans CJK SC", "Noto Sans CJK TC", "Pingfang SC", "Microsoft Yahei", "WenQuanYi Micro Hei", sans-serif;
      }
      body {
        overflow-x: hidden;
        background-color: #fff !important;
      }
      
      .mui-content {
        font-size: 13px;
        background-color: #fff;
      }
      
      .flex {
        display: flex;
        margin-top: 5px;
      }
      
      .flex-item {
        flex: 1;
      }
      
      .mui-select {
        width: 80%;
        margin-left: 10%;
        border: 1px solid #C0C0C0;
        border-radius: 6px;
        line-height: 24px;
        font-size: 14px;
        color: #ACACB4;
        text-indent: 6px;
        display: inline-block;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      
      .mui-select:before {
        top: 1px;
        right: 5px;
      }
      
      .goodsname-box,.barCode-box {
        flex: 7;
      }
      
      .mui-input-row input {
        margin-left: 18px;
        width: 80%;
        height: 26px;
        line-height: 24px;
        font-size: 14px;
      }
      
      .mui-input-row .mui-input-clear~.mui-icon-clear {
        top: 3px;
        right: 22px;
      }
      
      .search-btn-box {
        flex: 3;
      }
      
      .search-btn {
        border: 1px solid transparent;
        background-color: #ff6600;
        color: #fff;
        height: 28px;
        line-height: 26px;
        text-align: center;
        width: 80%;
        margin-left: 5%;
        border-radius: 2px;
        font-size: 14px;
      }
      
      .add-goods-type {
        margin-top: 0px;
        position: relative;
        height: 18px;
        line-height: 18px;
        vertical-align: middle;
      }
      
      .add-goods-type img {
        margin-top: 2px;
        margin-left: 10px;
        margin-right: 5px;
      }
      
      .shop-store {
        margin-left: 10px;
      }
      
      .add-goods-type span {
        padding-bottom: 2px;
      }
      /**/
      
      .purchase-link {
        float: right;
        margin-right: 10px;
      }
      /*列表*/
      
      .goods-list {
        list-style: none;
      }
      
      .goods-each {
        padding: 5px 5px;
        position: relative;
      }
      
      
      .img-box {
        flex: 2;
        position: relative;
      }
      
      .img-box img {
        height: 70px;
        width: 70px;
        vertical-align: middle;
        text-align: center;
      }
      .img-box .id{
      	position: absolute;
      	bottom: -7px;
      	width: 100%;
      	background-color: rgba(52, 52, 52, .5);
      	color: #FFFFFF;
      	text-align: center;
      }
      .goods-info-box {
        flex: 4;
        font-size: 12px;
        line-height: 18px;
      }
      
      .goods-info-box div {
        margin-left: 5px;
      }
      
      .goods-op-box {
        flex: 3;
        position: relative;
        height: 100%;
      }
      
      .choose-btn {
        width: 70%;
        position: absolute;
        bottom: -70px;
        right: 5px;
        text-align: center;
        background-color: #FF9800;
      	border-color: #FF9800;
      	color: #fff;
      }
      .choose-btn:active{
      	background-color: #e6c697;
      }
      /*滚动区*/
      
      .mui-scroll-wrapper {
        top: 100px;
        bottom: 2px;
      }
      
      .withdraw-next-btn {
        background-color: #5677FC;
        color: #fff;
      }
      /*加载*/
      
      .mui-pull-bottom-pocket {
        visibility: visible;
      }
      
      .mui-pull-bottom-pocket .mui-pull-loading {
        visibility: visible;
      }
      /**/
      
      .no {
        text-align: center;
        font-size: 14px;
        line-height: 30px;
      }
      /**/
      .bgGray{
      	background-color: #F8F8F8;
      }
    </style>
  </head>

  <body>
    <div class="mui-content">
      <div class="flex">
        <div class="flex-item">
          <div id="selsct-type" class="mui-select selsct-type">
            <span>全部尺寸</span>
          </div>
        </div>
        <div class="flex-item">
          <div id="select-tag" class="mui-select select-tag">
            <span>全部用途</span>
          </div>
        </div>
      </div>
      <div class="flex">
        <div class="goodsname-box">
          <div class="mui-input-row">
            <input type="text" class="mui-input-clear" placeholder="请输入名称">
          </div>
        </div>
        <div class="barCode-box">
          <div class="mui-input-row">
            <input type="text" class="mui-input-clear" placeholder="请输入货号">
          </div>
        </div>
        <div class="search-btn-box">
          <div class="search-btn">
            查找
          </div>
        </div>
      </div>
      <!---->
      <div class="add-goods-type">
        <span class="shop-store">门店仓库</span>
      </div>
      <!---->
      <div id="refreshContainer" class="mui-scroll-wrapper total-scroll-wrapper">
        <div class="mui-scroll">
          <ul class="goods-list mui-table-view mui-table-view-chevron">

          </ul>
          <!---->
          <script id="goodsLi" type="text/html">
            <%if(list.length>0){%>
            <% for(var i=0; i<list.length; i++) {%>
            <%if(i%2===0){%>
            		 <li class="goods-each">
            	<%} else {%>
            		 <li class="goods-each bgGray">
            	<%}%>
              <div class="flex">
                <div class="img-box">
                  <a href="#middlePopover" class="img-info" index="<%=i%>"><img src="<%=imgPath+list[i].wareGoods.cutImg%>" alt="" /></a>
                  <p class="id"><%=list[i].wareGoods.wareGoodsId%></p>
                </div>
                <div class="goods-info-box">
                  <div class="goods-name">
                    <%=list[i].wareGoods.name%>
                  </div>
                  <div class="goods-id">回收积分：
                    <%=list[i].wareGoods.score%>
                  </div>
                  <div class="goods-exchange">兑换积分：
                    <%=list[i].shopGoodsAmount.score !==null  ? list[i].shopGoodsAmount.score :  list[i].wareGoods.exchangeScore%>
                  </div>
                  <div class="goods-num">门店库存：
                    <%=list[i].shopGoodsAmount.amount%>
                  </div>
                </div>
                <div class="goods-op-box">
                  <button type="button" index="<%=i%>" class="choose-btn mui-btn">日志</button>
                </div>
              </div>
            </li>
            <%}%>
            <%}else{%>
            <p class="no">暂无数据</p>
            <%}%>
          </script>
          <!---->
          <!--加载中-->
          <div class="mui-pull-bottom-pocket">
            <div class="mui-pull">
            </div>
          </div>
          <!--加载中-->
        </div>
      </div>
    </div>
        <!--显示附加信息-->
    <div id="middlePopover" class="mui-popover">
			<div class="mui-popover-arrow"></div>
			<div class="mui-scroll-wrapper">
				<div class="mui-scroll">
					<p class="fullName">规格：<span></span></p>
					<p class="tags">用途：<span></span></p>
					<p class="score">积分：<span></span></p>
				</div>
			</div>
		</div>
		<!---->
  </body>
  <script src="../config.js"></script>
  <script src="../js/template-native.js" type="text/javascript" charset="utf-8"></script>
  <script src="../js/mui.min.js"></script>
  <script src="../js/mui.picker.js"></script>
  <script src="../js/mui.poppicker.js"></script>
  <script>
    (function($, doc) {
      var types = [{
          value: '',
          text: '全部尺寸'
        }],
        tags = [{
          value: '',
          text: '全部用途'
        }],
        currentShopObj,
        params = {
          tagIds: [],
          pageNo: 1,
          goodsName: '',
          barCode: '',
          goodsId: '',
          type: '',
          shopId: '',
          isAmountUpper: true
        },
        pageList = [],
        isLoading = false,
        pageTatol = 0,
        refundGoods = {
          goodsList: []
        }
      $.init({
        //				pullRefresh: {
        //					container: "#refreshContainer", //下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
        //					up: {
        //						height:100,
        //						auto:true,
        //						contentrefresh: '正在加载...',
        //						contentnomore:'没有更多数据了',
        //						callback: pullupRefresh
        //					}
        //				}
      })
      $.plusReady(function() {
        receiveParams()
        getTag()
        getTypes()
        getGoods(params)
      })
      $.ready(function() {
        bindEvent()
      })

      //滚动
      $('.mui-scroll-wrapper').scroll({
        scrollY: true,
        bounce: true,
        deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006，
      });

      function pullupRefresh() {
        mui('.mui-scroll-wrapper').scroll().reLayout();
        var that = this
        setTimeout(function() {
          that.endPullupToRefresh(true);
        }, 2000)
      }

      function receiveParams() {
        var self = plus.webview.currentWebview();
        currentShopObj = self.currentShopObj;
        params.shopId = self.currentShopObj.storeId
        params.storeName = self.currentShopObj.storeName
      }
      //设置加载状态
      function setLoading(type) {
        switch(type) {
          case 1:
            document.querySelector(".mui-pull").innerHTML = '<div class="mui-pull-loading mui-icon mui-spinner"></div>\
						<div class="mui-pull-caption">正在加载</div>'
            break
          case 2:
            document.querySelector(".mui-pull").innerHTML = '<div class="mui-pull-caption">没有更多</div>'
            break
          case 0:
            document.querySelector(".mui-pull").innerHTML = ''
            break
        }

      }

      function bindEvent() {
        document.querySelector(".search-btn").addEventListener('tap', function() {
        	params.pageNo = 1
          mui('.total-scroll-wrapper').scroll().scrollTo(0, 0, 100)
          params.goodsName = document.querySelector(".goodsname-box input").value
          params.barCode = document.querySelector(".barCode-box input").value
          ajaxGoods(params)
        })
        //滚动监听
        document.querySelector('.mui-scroll-wrapper').addEventListener('scrollend', function(e) {
          if(e.detail.y <= e.detail.maxScrollY) {
            if(isLoading === false) {
              if(++params.pageNo <= pageTatol) {
                ajaxGoods(params)
              } else {
                setLoading(2)
              }
            }
          }
        });
          //显示用途
        $('.goods-list').on('tap', '.img-info', function(e) {
          var ind = this.getAttribute('index')
          var typeFullName = pageList[ind].typeFullName
          var tags = ''
          pageList[ind].tags.forEach(function(item) {
            tags += item.tagDescribe + ' '
          })
          document.querySelector(".fullName span").innerText = typeFullName
          document.querySelector(".tags span").innerText = tags
          document.querySelector(".score span").innerText = pageList[ind].wareGoods.score
        })
        //日志
        mui('.goods-list').on('tap', '.choose-btn', function(e) {
          var index = this.getAttribute('index')
          var title = currentShopObj.storeName+'-库存日志'
          mui.openWindow({
            url: './stock-journal.html',
            id: 'stock-journal',
            styles: {
              top: 0, //新页面顶部位置
              bottom: 0, //新页面底部位置         
              titleNView: {
                titleText: title,
                titleColor: "#FFFFFF",
                titleSize: "17px",
                backgroundColor: "#FF6800",
                autoBackButton: true,
                //                homeButton: true,
                buttons: [{
                  float: "right",
                  fontSize: "18px",
                  fontSrc: "_www/fonts/iconfont.ttf", //wap2app内置字体文件
                  text: "\ue602",
                  onclick: function() {
                    var allPage = plus.webview.all()
                    var nowPage = plus.webview.getWebviewById('index')
                    for(var i = 0; i < allPage.length; i++) {
                      if(allPage[i].getURL() !== nowPage.getURL()) {
                        plus.webview.close(allPage[i]);
                      }
                    }
                  }
                }],
                splitLine: {
                  color: "#CCCCCC",
                  height: "1px"
                }
              }
            },
            extras: {
              currentShopObj: currentShopObj,
              goodsId: pageList[index].wareGoods.wareGoodsId,
              goodsName:pageList[index].wareGoods.name
            }
          })
        })
      }

      function setTypesSelect() {
        var typePicker = new $.PopPicker({
          layer: 2
        });
        typePicker.setData(types);
        var typeCheck = doc.querySelector("#selsct-type")
        typeCheck.addEventListener('tap', function(event) {
          typePicker.show(function(items) {
            if(items[1].text) {
              typeCheck.innerText = '' + items[0].text + "/" + items[1].text;
              params.type = items[1].value
            } else {
              typeCheck.innerText = '' + items[0].text
              params.type = ''
            }
            //返回 false 可以阻止选择框的关闭
            //return false; 
          });
        }, false)
      }

      function setTagsSelect() {
        var tagPicker = new $.PopPicker();
        tagPicker.setData(tags)
        var tagCheck = doc.querySelector('#select-tag')
        tagCheck.addEventListener('tap', function(event) {
          tagPicker.show(function(items) {
            tagCheck.innerHTML = "<span>" + items[0].text + "</span>"
            params.tagIds = []
            if(items[0].value) {
              params.tagIds.push(items[0].value)
            }
          })
        })
      }

      function getTypes() {
        config.ajax({
          type: "get",
          url: config.basePath + "/goodsType/get.do",
          async: true,
          success: function(res) {
            if(res.result === 1) {
              res.data.forEach(function(item) {
                var obj = {}
                obj.value = item.id
                obj.text = item.name
                var children = []
                if(item.children.length > 0) {
                  item.children.forEach(function(item1) {
                    var obj1 = {}
                    obj1.value = item1.id
                    obj1.text = item1.name
                    children.push(obj1)
                  })
                }
                obj.children = children
                types.push(obj)
              })
              setTimeout(function() {
                setTypesSelect()
              }, 0)
            }
          }
        });

      }

      function getTag() {
        config.ajax({
          type: 'post',
          url: config.basePath + '/tag/get.do',
          data: {
            tagDescribe: ''
          },
          headers: {
            'Content-Type': 'application/json'
          },
          success: function(res) {
            if(res.result === 1) {
              res.tags.forEach(function(item) {
                var obj = {}
                obj.value = item.tagId
                obj.text = item.tagDescribe
                tags.push(obj)
              })
            }
            setTimeout(function() {
              setTagsSelect()
            }, 0)
          }
        })

      }

      function getGoods(param) {
        if(pageList.length > 0) {
          setPage(pageList)
        } else {
          ajaxGoods(param)
        }
      }

      function ajaxGoods(param) {
        isLoading = true
        setLoading(1)
        config.ajax({
          type: "post",
          url: config.basePath + "/shopGoods/checkLike",
          async: true,
          data: param,
          headers: {
            'Content-Type': 'application/json'
          },
          success: function(res) {
            if(res.result === 1) {
              setLoading(0)
              pageTatol = res.pageList.pageTatol
              if(params.pageNo === 1) {
                if(res.pageList.data.length > 0) {
                  pageList = res.pageList.data
                  setPage(pageList)
                } else {
                  pageList = []
                  setPage([])
                }
              } else {
                res.pageList.data.forEach(function(item) {
                  pageList.push(item)
                })
                setPage(pageList)
              }
              setTimeout(function() {
                isLoading = false
              }, 1500)
            } else {
              setLoading(0)
              pageList = []
              setPage([])
              setTimeout(function() {
                isLoading = false
              }, 1500)
            }
          }
        });
      }
      //页面
      function setPage() {
        for(var j = 0; j < pageList.length; j++) {
          var ifEqual = false
          for(var p = 0; p < refundGoods.goodsList.length; p++) {
            if(pageList[j].wareGoods.wareGoodsId - 0 === refundGoods.goodsList[p].goodsId - 0) {
              pageList[j].isChoose = true
              pageList[j].refundAmount = refundGoods.goodsList[p].refundAmount
              ifEqual = true
              break
            }
          }
          if(!ifEqual) {
            pageList[j].isChoose = false
            pageList[j].refundAmount = 0
          }
        }
        console.log('pageList')
        console.log(JSON.stringify(pageList));
        var html = template('goodsLi', {
          list: pageList,
          imgPath: config.imgPath
        })
        $('.goods-list')[0].innerHTML = html
      }
    }(mui, document))
  </script>

</html>